#include "mainwindow.h"

#include <QDebug>
#include <QMessageBox>
#include <QPushButton>

#include "gamewindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),
      ui(new Ui::MainWindow) {
    ui->setupUi(this);

    QPixmap pix_1(":/img/background.png");
    int width = this->width();
    int height = this->height();
    ui->label_pic_1->setPixmap(pix_1.scaled(width, height));
    ui->label_pic_1->lower();

    QPixmap pix(":/img/gobang.png");
    int w = ui->label_pic->width();
    int h = ui->label_pic->height();
    ui->label_pic->setPixmap(pix.scaled(w, h, Qt::KeepAspectRatio));

    connect(ui->pushExit, &QPushButton::clicked, this, &MainWindow::close);
}

MainWindow::~MainWindow() {
    delete ui;
}

void MainWindow::on_pushPVE_clicked() {
    if (!ui->radioEasy->isChecked() && !ui->radioButton_2->isChecked() && !ui->radioButton_3->isChecked()) {
        QMessageBox::warning(this, "提示", " 请先选择难度！");
        return;
    }
    if (!ui->radioButton->isChecked() && !ui->radioButton_4->isChecked()) {
        QMessageBox::warning(this, "提示", "请选择先后手！");
        return;
    }
    this->hide();
    gameWindow = new GameWindow(1, difficulty, order);
    connect(gameWindow, &GameWindow::showMain, this, &MainWindow::returnHome);
    connect(gameWindow->finalDlg, &FinalDlg::returnHome, this, &MainWindow::returnHome);
    connect(gameWindow->finalDlg, &FinalDlg::showHistory, this, &MainWindow::hide);
    connect(gameWindow->returnBtn, &QPushButton::clicked, this, [=]() {
        gameWindow->close();
        this->show();
    });
    gameWindow->show();
}

void MainWindow::on_pushPVP_clicked() {
    this->hide();
    gameWindow = new GameWindow(0, difficulty, order);  // difficulty, order冗余
    connect(gameWindow, &GameWindow::showMain, this, &MainWindow::returnHome);
    connect(gameWindow->finalDlg, &FinalDlg::returnHome, this, &MainWindow::returnHome);
    connect(gameWindow->finalDlg, &FinalDlg::showHistory, this, &MainWindow::hide);
    connect(gameWindow->returnBtn, &QPushButton::clicked, this, [=]() {
        gameWindow->close();
        this->show();
    });
    gameWindow->show();
}

void MainWindow::returnHome() {
    gameWindow->finalDlg->close();
    gameWindow->close();
    this->show();
}

// 搜索深度2
void MainWindow::on_radioEasy_clicked() {
    difficulty = 2;
}

// 搜索深度3
void MainWindow::on_radioButton_2_clicked() {
    difficulty = 3;
}

// 搜索深度4
void MainWindow::on_radioButton_3_clicked() {
    difficulty = 4;
}

void MainWindow::on_radioButton_4_clicked() {
    order = 1;
}

void MainWindow::on_radioButton_clicked() {
    order = 0;
}
